package com.lkd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lkd.dto.PartnerDto;
import com.lkd.entity.Partner;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.PartnerService;
import com.lkd.vo.PartnerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user-service/partner")
public class PartnerController {
    @Autowired
    private PartnerService partnerService;

    @PostMapping
    public Result savePartner(@RequestBody PartnerDto dto) {
        Partner partner = BeanUtil.toBean(dto, Partner.class);
        partnerService.save(partner);
        return Result.success(true);
    }

    @GetMapping("/search")
    public Result findByPage(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        PageBean<PartnerVo> partnerVo = partnerService.findByPage(name, pageIndex, pageSize);
        return Result.success(partnerVo);
    }

    @PutMapping
    public Result update(@RequestBody PartnerDto dto) {
        Partner partner = BeanUtil.toBean(dto, Partner.class);
        partnerService.update(partner);
        return Result.success(true);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        PartnerVo vo = partnerService.findById(id);
        return Result.success(vo);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        partnerService.delete(id);
        return Result.success(true);
    }

}
